package com.xwd.dao.impl;

import com.xwd.dao.DBUtils;
import com.xwd.dao.MenuDao;
import com.xwd.domain.Menu;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtils implements MenuDao {
    private String sql =null;

    @Override
    public List<Menu> getMenuList() {
        List<Menu> mList = new ArrayList<>();
        try {
            sql="select * from menu";
            resultSet = query(sql, null);
            while (resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));
                mList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return mList;
    }

    @Override
    public List<Menu> getMenuListByRoleId(Integer roleId) {
        List<Menu> list = new ArrayList<>();
        try {
            sql=" SELECT * FROM menu m,middle mi,role r WHERE mi.`menuid`=m.`menuid` AND r.`roleid`=mi.`roleid` AND r.roleid=? ";
            List param = new ArrayList();
            param.add(roleId);
            resultSet = query(sql, param);
            while (resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));
                list.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }
}
